package net.yxsoft.controller.pc.sysmgt.infrastructure;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Duang;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Record;
import net.yxsoft.bean.JsTreeNode;
import net.yxsoft.bean.JsTreeNodeFieldKey;
import net.yxsoft.bean.PowerImportModel;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.controller.pc.sysmgt.common.validator.DataModelValidator;
import net.yxsoft.controller.pc.sysmgt.common.validator.PartyValidator;
import net.yxsoft.enumclass.LockEnumCls;
import net.yxsoft.enumclass.infrastructure.CodeSaveModeEnumCls;
import net.yxsoft.enumclass.infrastructure.DbFieldTypeEnumCls;
import net.yxsoft.enumclass.infrastructure.DbFieldTypeEnumCls_mysql;
import net.yxsoft.kit.JsTreeNodeKit;
import net.yxsoft.model.infrastructure.TsCode;
import net.yxsoft.model.infrastructure.TsDatamodelProperties;
import net.yxsoft.model.infrastructure.TsParty;
import net.yxsoft.render.PoiRender;
import net.yxsoft.resource.DbSourceRes;
import net.yxsoft.resource.Res;
import net.yxsoft.resource.TsCodeRes;
import net.yxsoft.service.infrastructure.CodeService;
import net.yxsoft.service.infrastructure.DataModelService;
import net.yxsoft.service.infrastructure.DataModelService_oracle;
import net.yxsoft.service.infrastructure.UDMKeyService;
import rapid.core.ContextStateEnum;
import rapid.core.TResult;
import rapid.jfinal.Pagination;
import rapid.util.StrKit;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @MethodName: 数据模型管理
* @Description:
* @param: null
* @return * @return null
* @throws
* @date 2023/8/31 15:25
* @author jhf
*/
public class DataModelController extends AuthController {



  static final DataModelService srv = DataModelService.me;
  static final CodeService code_srv = CodeService.INSTANCE;

  public void index() throws IllegalAccessException {

    List<CodeSaveModeEnumCls> codeSaveMode = CodeSaveModeEnumCls.defaults(CodeSaveModeEnumCls.class);

    setAttr("codeSaveMode", codeSaveMode);
    render("datamodel.html");
  }

  /**
  * @MethodName: lcyExportModel
  * @Description: 调用导出页面
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void lcyExportModel() throws IllegalAccessException {
    List<TsParty> modeltype = srv.getPartyNameList();
    setAttr("modeltype", modeltype);
    render("lcy_export_model.html");

  }
  /**
  * @MethodName: loadModelType
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void loadModelType() {
    renderJson(code_srv.getCodesByParentCustomId("MXLB","MXLB","1","MODEL"));
  }
  /**
  * @MethodName: loadModelSize
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void loadModelSize() {

    renderJson(code_srv.getCodesByParentCustomId("MD","MD","1","MODEL"));
  }
  /**
  * @MethodName: getFieldTypeResouceAdd
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void getFieldTypeResouceAdd() {
    renderJson(DbFieldTypeEnumCls_mysql.COLLECTS_DEFAULTS);
  }
  /**
  * @MethodName: getFieldTypeResouceUpdate
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void getFieldTypeResouceUpdate() {
    renderJson(DbFieldTypeEnumCls.COLLECTS_VARCHAR2);
  }

  /**
  * @MethodName: loadDataModelTree
  * @Description: 初始化模型树
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void loadDataModelTree() {
    renderJson(srv.loadDataModelTree());
  }

  /**
  * @MethodName: loadParentModel
  * @Description: 获取一级模型
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void loadParentModel() {
    renderJson(srv.loadParentModel());
  }


  /**
  * @MethodName: saveModel
  * @Description: 保存模型
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  @Before({POST.class, PartyValidator.class})
  public void saveModel() {
    TsParty party = getModel(TsParty.class, "");
    if(srv.findfirst(party.getName(), party.getCnname()) == null) {
      party.setCreatedBy(getLoginUser().getId());
      renderJson(Duang.duang(DataModelService_oracle.class).saveModel(party));
    } else {
      TResult result = new TResult();
      result.setState(ContextStateEnum.FAIL);
      result.setMessage("模型名称不能重复。");
      renderJson(result);
    }
  }

  /**
  * @MethodName: removeModel
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  @Before(POST.class)
  public void removeModel() {
    String _key = getPara("partyKey");
    renderJson(Duang.duang(DataModelService_oracle.class).removeModel(_key));
  }

  /**
  * @MethodName: getPartyById
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void getPartyById() {

    TsParty model = new TsParty().findById(this.getPara("conflictId"));
    renderJson(model);
  }
  /**
  * @MethodName: getjsTreeByBusCodes
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:25
  * @author jhf
  */
  public void getjsTreeByBusCodes() throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
    JsTreeNodeFieldKey tnfk = new JsTreeNodeFieldKey("Codename", "Codename", "Parentname");
    tnfk.setLeafKey("Codename");
    tnfk.setLeafValue(TsCodeRes.NAME_BUSINESS);

    List<TsCode> codelist = TsCode.dao.find("SELECT * FROM TS_CODE WHERE CODEID LIKE '003%' ORDER BY SORTNO,CODEID");

    List<JsTreeNode> treelist = JsTreeNodeKit.SerializeJsTreeData(tnfk, codelist);

    renderJson(treelist);

  }

  /**
  * @MethodName: getDataModelFields
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void getDataModelFields() {



    Kv cond = Kv.by("modelId", getPara("modelId"));
    String state = getPara("state", null);
    String attrNum = getPara("attrNum", null);

    if(StrKit.hasValue(state)) {
      cond.set("state", state);
    }

    if(StrKit.hasValue(attrNum)) {
      cond.set("attrNum", attrNum);
    }



    renderJson(srv.query("model.datamodel", cond, Pagination.by(this)));


  }
  /**
  * @MethodName: getDataModelById
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void getDataModelById() {
    TsDatamodelProperties model = new TsDatamodelProperties().findById(this.getPara("Id"));
    renderJson(model);

  }
  /**
  * @MethodName: addDataModel
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  @Before({POST.class, DataModelValidator.class})
  public void addDataModel() throws Exception {
    TsDatamodelProperties _m = getModel(TsDatamodelProperties.class, "");
    renderJson(Duang.duang(DataModelService.class).addDataModel(_m));
  }
  /**
  * @MethodName: updateDataModel
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  @Before({POST.class, DataModelValidator.class})
  public void updateDataModel() {
    TsDatamodelProperties _m = getModel(TsDatamodelProperties.class, "");
    renderJson(Duang.duang(DataModelService.class).updateDataModel(_m));
  }
  /**
  * @MethodName: removeDataModel
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  @Before(POST.class)
  public void removeDataModel() {
    renderJson(Duang.duang(DataModelService.class).removeDataModel(getPara("ID")));
  }
  /**
  * @MethodName: resetDataModel
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  @Before(POST.class)
  public void resetDataModel() {
    String _key = getPara("ID");
    renderJson(Duang.duang(DataModelService.class).resetDataModel(_key));
  }

  /**
  * @MethodName: getUDMKeys
  * @Description: 设置主键
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void getUDMKeys() {
    Kv cond = Kv.by("modelId", getPara("modelId"));
    renderJson(srv.query("udKey.udKeyPageSelect", cond, Pagination.by(this)));
  }
  /**
  * @MethodName: getDataModelUDMKeyList
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void getDataModelUDMKeyList() {
    String _id = getPara("modelId");
    String name = getPara("name");
    List<Record> _source = UDMKeyService.getDataModelUDMKeyList(_id, name);
    renderJson(_source);
  }

  /**
  * @MethodName: powerImport
  * @Description: 万能导入
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void powerImport() {
    PowerImportModel model = getBean(PowerImportModel.class, "", true);


      renderJson(srv.powerImportExl(model, getLoginUser(),getPara("rowid")));


  }
  /**
  * @MethodName: getImpWebsocketUrl
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void getImpWebsocketUrl() {
    Map map = new HashMap();
    map.put("url", Res.WebSocketUrl);
    map.put("userid", getLoginUser().getId());

    PowerImportModel model = getBean(PowerImportModel.class, "", true);
    TResult result=srv.powerImportExlCk(model, getLoginUser());
    if(result.isSuccess()){
      result.setData(map);
    }
    renderJson(result);


  }

  /**
  * @MethodName: powerImportByRowid
  * @Description: 子模型万能导入
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void powerImportByRowid() {
    PowerImportModel model = getBean(PowerImportModel.class, "", true);
    renderJson(srv.powerImportExl(model,getLoginUser(),getPara("row_id")));
  }

  /**
  * @MethodName: exportModelName
  * @Description: 导出模型信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void exportModelName() {

    String model = getPara("keys");
    String _tvals = getPara("_tvals");

    List<TsDatamodelProperties> datamodelProperties = srv.exportModelName1(model, _tvals);
    String[] clumns = {"ATTRIB_NAME", "ATTRIB_DESCN", "ATTRIB_TYPE", "ATTRIB_TYPE_DESC", "FORMULA", "DEFAULT_VAL", "PARTY_SIZE"};
    String[] heades = {"字段名", "字段描述", "字段类型", "字段类型描述", "计算公式", "缺省值", "模型大小"};
    render(PoiRender.me(datamodelProperties).fileName(model + ".xls").sheetName("model").headers(heades).columns(clumns).cellWidth(5000).headerRow(1));

  }




  /**
  * @MethodName: getDataModelUDMKeyChooseByParty1
  * @Description: 根据模型名称获取模型字段
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:26
  * @author jhf
  */
  public void getDataModelUDMKeyChooseByParty1() {
    final String _name = getPara("partylName");

    if(_name == null) {
      renderJson(-99);
    }

    List<TsDatamodelProperties> _source = srv.exportModelName(_name);
    renderJson(_source);
  }

  /**
  * @MethodName: saveUDMKey
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  @Before(POST.class)
  public void saveUDMKey() {

    String _modelId = getPara("modelId");
    String _title = getPara("title");
    String _attribs = getPara("attribs");

    renderJson(UDMKeyService.saveUDMKey(_modelId, _title, _attribs, getLoginUser()));
  }
  /**
  * @MethodName: removeUDMKey
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  @Before(POST.class)
  public void removeUDMKey() {
    String _ids = getPara("conflictIds");

    renderJson(UDMKeyService.removeUDMKey(_ids));
  }



  /**
  * @MethodName: getModelTableByPicker
  * @Description: 万能导入picker获取所有模型表
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void getModelTableByPicker() {
    renderJson(srv.getTsPartyByPicker(getPara("modelId")));
  }

  /**
  * @MethodName: getModelUDMkeyByPicker
  * @Description: 万能导入picker获取所有模型表的主键
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void getModelUDMkeyByPicker() {
    renderJson(srv.getUdmKeyPickerByModelId(getPara("modelId")));
  }

  /**
  * @MethodName: getRuleCode
  * @Description: 万能导入更新规则
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void getRuleCode() {
    renderJson(srv.getUpdateRule());
  }
  /**
  * @MethodName: updateXlsFile
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  @Clear
  public void updateXlsFile() {
    renderJson(uploadFile());
  }
  /**
  * @MethodName: getAttrrbStateByEm
  * @Description:
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void getAttrrbStateByEm() {
    renderJson(LockEnumCls.defaults(LockEnumCls.class));
  }
}
